package com.lcxw.hxzbadmin.common;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class YwpdAdminConfig implements WebMvcConfigurer {

    @Bean
    public YwpdInterceptor getYwpdInterceptor() {
        return new YwpdInterceptor();
    }


    @Bean
    public FilterRegistrationBean repeatedlyReadFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        RepeatedlyReadFilter repeatedlyReadFilter = new RepeatedlyReadFilter();
        registration.setFilter(repeatedlyReadFilter);
        registration.addUrlPatterns("/*");
        return registration;
    }

    /**
     * 拦截器的配置
     *
     * @param registry 入参
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //需要拦截的路径，/**表示需要拦截所有请求
        String[] addPathPatterns = {"/**"};
        //不需要拦截的路径
        String[] excludePathPaterns = {
            "/admin/login",
            "/feign/**"
//            "/admin/config/**",
//            "/admin/database/**"
        };
        //注册一个登录拦截器

        registry.addInterceptor(getYwpdInterceptor())
            .addPathPatterns(addPathPatterns)
            .excludePathPatterns(excludePathPaterns);
        //注册一个权限拦截器  如果有多个拦截器 ，只需要添加以下一行代码
        //registry.addInterceptor(new LoginInterceptor())
        // .addPathPatterns(addPathPatterns)
        // .excludePathPatterns(excludePathPatterns);

    }
}
